package com.haisen.dataobject.query;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

public class ItemDynaSqlProvider {

    public String selectWithItemSql(ItemQuery itemQuery) {

        return new SQL() {
            {

                SELECT("a.id uid ,a.id ,a.area_id as countryId,a.title,a.sell_point as sellPoint,a.price,a.num,a.barcode,a.image,a.cat_id as catId,b.name as catName,a.up");
                FROM(" tb_item a,tb_item_cat b ");
                WHERE(" a.cat_id = b.id ");
                if (!StringUtils.isEmpty(itemQuery.getAreaId())) {
                    WHERE("a.area_id like '%'+#{areaId}+'%' ");
                }
                if(!StringUtils.isEmpty(itemQuery.getTitle())) {
                    WHERE("a.title=#{title} ");
                }
                if(!StringUtils.isEmpty(itemQuery.getSellPoint())) {
                    WHERE("a.sell_point=#{sellPoint} ");
                }
            }
        }.toString();
    }


    public String selectCountWithItemSql(ItemQuery itemQuery) {

        return new SQL() {
            {

                SELECT("count(*)");
                FROM(" tb_item a,tb_item_cat b ");
                WHERE(" a.cat_id = b.id ");
                if (!StringUtils.isEmpty(itemQuery.getAreaId())) {
                    WHERE("a.area_id like '%'+#{areaId}+'%' ");
                }
                if(!StringUtils.isEmpty(itemQuery.getTitle())) {
                    WHERE("a.title=#{title} ");
                }
                if(!StringUtils.isEmpty(itemQuery.getSellPoint())) {
                    WHERE("a.sell_point=#{sellPoint} ");
                }
            }
        }.toString();
    }


}
